package com.firefly.service.repository.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.ums.po.DepartmentAdminPo;
import com.firefly.dao.dao.ums.service.DepartmentAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 部门用户对应关系
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/11/30
 **/
@Repository
public class DepartmentAdminRepository {
    private final DepartmentAdminService departmentAdminService;
    @Autowired
    public DepartmentAdminRepository(DepartmentAdminService departmentAdminService){
        this.departmentAdminService = departmentAdminService;
    }
    // 获取下属用户数量
    public Long getUserNum(List<Integer> ids){
        QueryWrapper<DepartmentAdminPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("department_id", ids);
        return departmentAdminService.count(queryWrapper);
    }
    public void saveBatchByAdminId(int adminId,List<Integer> departmentIds){
        List<DepartmentAdminPo> insertDepartment = new ArrayList<>();
        departmentIds.forEach(item -> {
            DepartmentAdminPo tmp = new DepartmentAdminPo();
            tmp.setDepartmentId(item);
            tmp.setAdminId(adminId);
            insertDepartment.add(tmp);
        });
        departmentAdminService.saveBatch(insertDepartment);
    }
    public void removeByAdminId(int adminId){
        QueryWrapper<DepartmentAdminPo> queryDepartmentWrapper = new QueryWrapper<>();
        queryDepartmentWrapper.eq("admin_id", adminId);
        departmentAdminService.remove(queryDepartmentWrapper);
    }
    public void removeByAdminIds(List<Integer> adminIds){
        QueryWrapper<DepartmentAdminPo> queryDepartmentWrapper = new QueryWrapper<>();
        queryDepartmentWrapper.in("admin_id", adminIds);
        departmentAdminService.remove(queryDepartmentWrapper);
    }
}
